//
//  HsFormUtil.m
//  Hybrid-iPad
//
//  Created by chenhf on 14-5-23.
//  Copyright (c) 2014年 hundsun. All rights reserved.
//

#import "HsFormUtil.h"

@implementation HsFormUtil

+(BOOL)isStringEmpty:(NSString*)str{
    if(str==nil||[[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@""]){
        return YES;
    }
    return NO;
}

+(BOOL)isNaN:(NSString *)str{
    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"[^0-9]" options:0 error:nil];
    if([regex numberOfMatchesInString:str options:NSMatchingReportCompletion range:NSMakeRange(0, str.length)]>0){
        return YES;
    }
    return NO;
}

+(BOOL)isValidStylePostcode:(NSString*)postcode{
    if([HsFormUtil isStringEmpty:postcode]){
        return NO;
    }
    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"^\\d{6}$" options:0 error:nil];
    if([regex numberOfMatchesInString:postcode options:NSMatchingReportCompletion range:NSMakeRange(0, postcode.length)]>0){
        return YES;
    }
    return NO;
}

+(BOOL)isValidStyleEmail:(NSString*)email{
    if([HsFormUtil isStringEmpty:email]){
        return NO;
    }
    
    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$" options:0 error:nil];
    if([regex numberOfMatchesInString:email options:NSMatchingReportCompletion range:NSMakeRange(0, email.length)]>0){
        return YES;
    }
    return NO;
}

+(BOOL)isValidStyleMobile:(NSString*)phone{
    if([HsFormUtil isStringEmpty:phone]){
        return NO;
    }
    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"^1[34578][(?:0-9)]{9}$" options:0 error:nil];
    if([regex numberOfMatchesInString:phone options:NSMatchingReportCompletion range:NSMakeRange(0, phone.length)]>0){
        return YES;
    }
    return NO;
}

+(BOOL)isValidStyleTel:(NSString*)tel{
    if([HsFormUtil isStringEmpty:tel]){
        return NO;
    }
    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"^[\\d-]+$" options:0 error:nil];
    if([regex numberOfMatchesInString:tel options:NSMatchingReportCompletion range:NSMakeRange(0, tel.length)]>0){
        return YES;
    }
    return NO;
}

+(BOOL)isValidStyleIdcard:(NSString*)idcard{
    int len = idcard.length;
    unichar checkcode;
    if (len == 15){
        if([HsFormUtil isNaN:idcard]){
            //alert('\u8eab\u4efd\u8bc1\u53f7\u7801\u65e0\u6548\uff01');
            return NO;
        }
    }else if (len == 18){
        checkcode=[idcard characterAtIndex:17];
        if([HsFormUtil isNaN:[idcard substringToIndex:17]]||((checkcode<'0'||checkcode>'9')&&checkcode&&checkcode!='X'&&checkcode!='x')){
            //alert('\u8eab\u4efd\u8bc1\u53f7\u7801\u65e0\u6548\uff01');
            return NO;
        }
    }else {
        //alert('\u8eab\u4efd\u8bc1\u53f7\u7801\u65e0\u6548\uff01');
        return NO;
    }

    // 验证身份证合法性
    int weight[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    int sum=0;
    for(int i=0;i<17;i++){
        int charToInt = (int)[idcard characterAtIndex:i]-(int)'0';
        sum += (charToInt)*weight[i];
    }
    int remainder = sum%11;
    unichar checkCodes[] = {'1','0','X','9','8','7','6','5','4','3','2'};
    
    if(checkcode==checkCodes[remainder]){
        return YES;
    }else if(remainder==2&&checkcode=='x'){
        return YES;
    }
    return NO;
}

+(int)moneyMaxLegthForInteger{
    return 12;
}

+(int)moneyMaxLegthForFloat{
    return 2;
}

+(NSString *)moneyForZH:(NSString *)money{
    NSMutableString* result = [NSMutableString string];
    NSString* strUnit = @"仟佰拾亿仟佰拾万仟佰拾元角分";
    NSRange range = [money rangeOfString:@"."];
    money = [NSString stringWithFormat:@"%@00",money];
    if(range.length>0){
        money = [NSString stringWithFormat:@"%@%@",[money substringToIndex:range.location],[money substringWithRange:NSMakeRange(range.location+1, 2)]];
    }

    strUnit = [strUnit substringFromIndex:(strUnit.length-money.length)];
    //'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'
    NSString* digitZh[10] = {@"零",@"壹",@"贰",@"叁",@"肆",@"伍",@"陆",@"柒",@"捌",@"玖"};
    for (int i=0; i < money.length; i++){
        int digit = ((int)[money characterAtIndex:i]-(int)'0');
        [result appendString:digitZh[digit]];
        [result appendString:[strUnit substringWithRange:NSMakeRange(i, 1)]];
    }
    
    //result money
    money = [result stringByReplacingOccurrencesOfString:@"零角零分" withString:@"整" options: NSRegularExpressionSearch range:NSMakeRange(result.length>4?(result.length-5):0, result.length>4?5:result.length)];
    money = [money stringByReplacingOccurrencesOfString:@"零[仟佰拾]" withString:@"零" options: NSRegularExpressionSearch range:NSMakeRange(0, money.length)];
    money = [money stringByReplacingOccurrencesOfString:@"零{2,}" withString:@"零" options: NSRegularExpressionSearch range:NSMakeRange(0, money.length)];
    money = [money stringByReplacingOccurrencesOfString:@"零([亿|万])" withString:@"$1" options: NSRegularExpressionSearch range:NSMakeRange(0, money.length)];
    money = [money stringByReplacingOccurrencesOfString:@"零+元" withString:@"元" options: NSRegularExpressionSearch range:NSMakeRange(0, money.length)];
    money = [money stringByReplacingOccurrencesOfString:@"亿零{0,3}万" withString:@"亿" options: NSRegularExpressionSearch range:NSMakeRange(0, money.length)];
    money = [money stringByReplacingOccurrencesOfString:@"^元" withString:@"零元" options: NSRegularExpressionSearch range:NSMakeRange(0, money.length)];
    return money;
}

+(void)appendPostKey:(NSString*)key Value:(NSString*)value To:(NSMutableDictionary*)postData{
    [postData setObject:value==nil?@"":value forKey:key];
}

+(void)appendKey:(NSString*)key Value:(NSString*)value To:(NSMutableString*)postData{
    [postData appendString:key];
    [postData appendString:@"="];
    [postData appendString:value==nil?@"":value];
    [postData appendString:@"&"];
}

@end
